<?php

class Model_Comment extends Model_Database
{
	var $table_comment = 'comment';
	var $table_article = 'article';
	var $table_user = 'user';
	
	public function get_list($offset = 0, $limit = 20){
		$sql = "select c.cid, c.aid, c.uid, c.nick, c.comment, c.ip, c.addtime, c.type, c.status, a.title, u.user, u.nick as user_nick ";
		$sql .= "from {$this->table_comment} as c ";
		$sql .= "left join {$this->table_article} as a on a.aid=c.aid ";
		$sql .= "left join {$this->table_user} as u on u.uid=c.uid ";
		$sql .= "where c.del=0 and a.del=0 order by c.cid desc "; // and u.del=0 
		$sql .= "limit :offset, :limit";
		$query = DB::query(Database::SELECT, $sql);
		$query->parameters(array(
			':offset' => $offset,
			':limit' => $limit
		));
		return $query->execute()->as_array();
	}
	
	public function get_by_aid($aid, $offset = 0, $limit = 10){
		$sql = "select cid, aid, uid, nick, comment, ip, addtime, type, status from {$this->table_comment} ";
		$sql .= "where aid=:aid and del=0 ";
		$sql .= "order by cid desc limit :offset, :limit";
		$query = DB::query(Database::SELECT, $sql);
		$query->parameters(array(
			':aid' => $aid,
			':offset' => $offset,
			':limit' => $limit
		));
		return $query->execute()->as_array();
	}
	
	public function get_count_by_aid($aid){
		$sql = "select cid from {$this->table_comment} where aid=:aid and del=0";
		$query = DB::query(Database::SELECT, $sql);
		$query->parameters(array(
			':aid' => $aid
		));
		return $query->execute()->count();
	}
	
	public function get_page_by_aid ($aid, $page = 1, $page_size = 10){
		$offset = ($page - 1) * $page_size;
		$comments = $this->get_by_aid($aid, $offset, $page_size);
		$total = $this->get_count_by_aid($aid);
		$page_num = ceil($total / $page_size);
		$page_html = '';
		if ($page_num == 1){
		}elseif($page_num <= 4) {
			$page_prev = $page - 1;
			$page_next = $page + 1;
			$page_html .= '<a class="page S_bg1" href="/detail/index/' . $aid . '/' . $page_prev . '" title="上一页"><span>上一页</span></a>';
			for ($_page = 1; $_page <= $page_num; $_page++){
				if ($_page == $page){
					$page_html .= '<span class="comment-page-cur">' . $_page . '</span>';
				}else {
					$page_html .= '<a class="page S_bg1" href="/detail/index/' . $aid . '/' . $_page . '">' . $_page . '</a>';
				}
			}
			$page_html .= '<a class="page S_bg1" href="/detail/index/' . $aid . '/' . $page_next . '" title="下一页"><span>下一页</span></a>';
		}else {
			$page_prev = $page - 1;
			$page_next = $page + 1;
			$page_html .= '<a class="page S_bg1" href="/detail/index/' . $aid . '/' . $page_prev . '" title="上一页"><span>上一页</span></a>';
			if ($page_prev != 0){
				$page_html .= '<span>...<span>';
				$page_html .= '<a class="page S_bg1" href="/detail/index/' . $aid . '/' . $page_prev . '">' . $page_prev . '</a>';
			}
			$page_html .= '<span class="comment-page-cur">' . $page . '</span>';
			if ($page_next != $page_num + 1){
				$page_html .= '<a class="page S_bg1" href="/detail/index/' . $aid . '/' . $page_next . '">' . $page_next . '</a>';
				$page_html .= '<span>...<span>';
			}
			$page_html .= '<a class="page S_bg1" href="/detail/index/' . $aid . '/' . $page_next . '" title="下一页"><span>下一页</span></a>';
		}
		
		$result['result'] = $comments;
		$result['page'] = $page_html;
		return $result;
	}
	
	public function add($data){
		$aid = $data['aid'];
		$uid = $data['uid'];
		$nick = addslashes(trim($data['nick']));
		$comment = addslashes(trim($data['comment']));
		$ip = $data['ip'];
		$addtime = date('Y-m-d H:i:s');
		$type = $data['type']; // 1 网站注册用户 2微博用户 3匿名
		
		$sql = "insert into {$this->table_comment} (aid, uid, nick, comment, ip, addtime, type)";
		$sql .= "values(:aid, :uid, :nick, :comment, :ip, :addtime, :type)";
		$query = DB::query(Database::INSERT, $sql);
		$query->parameters(array(
			':aid' => $aid,
			':uid' => $uid,
			':nick' => $nick,
			':comment' => $comment,
			':ip' => $ip,
			':addtime' => $addtime,
			':type' => $type
		));
		list($cid) = $query->execute();
		return $cid;
	}
	
}
